<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use User\Api\UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller {

    /**
     * 后台用户登录
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){
            // print_R($_POST);
            // die;
            /* 检测验证码 TODO: */
              // var_dump( check_verify($verify));
              //   die;
            // print_R($verify);
            // die;
            if(!check_verify($verify)){
              
                // $this->error('验证码输入错误！');
            }

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            if(0 < $uid){ //UC登录成功
                /* 登录用户 */
                $Member = D('Member');
                if($Member->login($uid)){ //登录用户
                    //TODO:跳转到登录前页面
                   // echo "<pre>";
                   // print_r($_SESSION);
                   // die;
                  $this->display("Index/index");  
                } else {
                    $this->error($Member->getError());
                }

            } else { //登录失败
                switch($uid) {
                    case -1: 
                    // $yz       = array();
                    // $yz['yz'] = "asdasdasda";
                    // $this->assign("yz",$yz);
                    // $this->display("Public/login");
                    $error = "账户不存在";
                     break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }
        } else {
            if(is_login()){
                $this->redirect('Index/index');
            }else{
                /* 读取数据库中的配置 */
                $config	=	S('DB_CONFIG_DATA');
                if(!$config){
                    $config	=	D('Config')->lists();
                    S('DB_CONFIG_DATA',$config);
                }
                C($config); //添加配置
                
                $this->display();
            }
        }
    }
    // 测试
 

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            D('Member')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }

    //验证账号是否存在
    public function publicyz()
    {   
        // echo "<pre>";
        // print_r($_POST);
        // echo 1;
      
        $username = $_POST['username'];
        $password = $_POST['password'];
        $verify   = $_POST['verify'];

        $model    = M("ucenter_member");
        $list     = $model->field("username,password")->select();

        // print_r($list);
        foreach($list as $v){
            $listname[]     = $v['username'];
            $listpassword[] = $v['password'];

        }
        // print_R($listname);
        // print_R($listpassword);
         // 判断用户账号
         // 1 账号错误！
         // 2 密码错误！
         // 3 跳转


         $User = new UserApi;
         $uid = $User->login($username, $password);
         // print_r($uid);
         if($uid>0){
             if(!check_verify($verify)){
                  
                 echo 4;      
                // $this->error('验证码输入错误！');
            }else{
                 // print_r($username);
                
                 // $username1 = $model->select();
                 $user1 = $model->where(array("username"=>"$username"))->select();
                 // echo "<pre>";
                 // print_r($user1);
                 // print_R($user1[0]['username']);
                 // echo "<br>";
                 // print_R($user1[0]['id']);
                 // echo "<br>";
                 $_SESSION['home']['user_auth']['username'] = $user1[0]['username']; 
                 $_SESSION['home']['user_auth']['uid'] = $user1[0]['id']; 
                 // echo "<pre>";
                 // print_r($_SESSION);
                 echo 3;

            }
         }else{
             switch($uid) {
                    case -1: echo 1; break; //系统级别禁用
                    case -2: echo 2; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
         }

         // if(in_array($username,$listname)){
           
         //    if(in_array(md5($password),$listpassword)){
         //                $this->display("Index/index");         
         //      }else{
         //                echo 1;
         //      }
         // }else{
         //                echo 2;
         // }

        // $username = $_POST['csname'];
        // $list     = array();
        // $model = M("Member");
        // $list  =   $model->field('nickname')->select();
        // // print_r($list);
        // foreach($list as $v)
        // {  
        //     $listname[] = $v['nickname'];
        // };
        //     // print_r($listname);
        //     // print_r($username);

        // if(in_array($username,$listname)){
        //     echo true;
        // }else{
        //     echo 0;
        // }
    }

}
